﻿package app.view.components.guiDima.sky 
{
	import app.view.ApplicationMediator;
	import app.view.events.KeyboardEventD;
	import flash.display.Sprite;
	import flash.events.Event;
	import gs.TweenLite;
	
	/**
	 * Область с рандомно плывущими облаками
	 * @author dmitryi koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class SkyFieldDBase extends Sprite
	{
		// app.view.components.guiDima.sky.SkyFieldDBase
		//	public
		
		//	private
		public var bgdSky:Sprite;
		
		
		// валовые облака
		public var sky1:Sprite;
		public var sky2:Sprite;
		public var sky3:Sprite;
		public var sky4:Sprite;
		public var sky5:Sprite;
		public var sky6:Sprite;
		// облачка с сообщениями
		public var sky_01:SkyDBase;
		public var sky_02:SkyDBase;
		public var sky_03:SkyDBase;
		public var sky_04:SkyDBase;
		public var sky_05:SkyDBase;
		public var sky_06:SkyDBase;
		public var sky_07:SkyDBase;
		public var sky_08:SkyDBase;
		public var sky_09:SkyDBase;
		
		private var _tweenSky1:TweenLite;
		private var _tweenSky2:TweenLite;
		private var _tweenSky3:TweenLite;
		private var _tweenSky4:TweenLite;
		private var _tweenSky5:TweenLite;
		private var _tweenSky6:TweenLite;
		//---------------
		private var _tweenSky01:TweenLite;
		private var _tweenSky02:TweenLite;
		private var _tweenSky03:TweenLite;
		private var _tweenSky04:TweenLite;
		private var _tweenSky05:TweenLite;
		private var _tweenSky06:TweenLite;
		private var _tweenSky07:TweenLite;
		private var _tweenSky08:TweenLite;
		private var _tweenSky09:TweenLite;
		
		
		//	const
		private static const SKY_GAP_LEFT:Number = -100;
		private static var SKY_GAP_RIGHT:Number// = bgdSky.width + 100;
		
		private static const SKY_M_GAP_LEFT:Number = -400;
		private static var SKY_M_GAP_RIGHT:Number// = bgdSky.width + 400;
		
		private static var SKY_M_GAP_OUT_X:Number// = bgdSky.width/2;
		private static const SKY_M_GAP_OUT_Y:Number = -300;
		
		private var _time1:Number=400;
		private var _time2:Number=500;
		private var _time3:Number = 600;
		
		private var _speed1:Number=5; // пиксели в секунду
		private var _speed2:Number=6;
		private var _speed3:Number=7;
		
		private var _message:Array;
		private var _itemsSky:Array;
		
		//	CONSTRUCTOR
		public function SkyFieldDBase() 
		{
			init();
		}
		
		//	METHODS
		//	private
		private function init():void
		{
			SKY_GAP_RIGHT = bgdSky.width + 100;
			SKY_M_GAP_RIGHT = bgdSky.width + 400;
			SKY_M_GAP_OUT_X = bgdSky.width / 2;
			
			//sky1.x = SKY_GAP_LEFT;
			
			_message = new Array();
			_message.push('adflkkj 23452345');
			_message.push('fgh fdghd sdg sdg ser5647 4567');
			_message.push('eytrg wet 345 45  545 54цеп ццуке ц435 ц45е ');
			_message.push('вфыапф фукеп5454');
			_message.push('98657 апо');
			_message.push('ывап жщшфа98ф фыало');
			_message.push('dasfg;lkj90 sdfgi 0 dfgij adf0');
			_message.push(';alskdj adsfas 909345234 sdflgj');
			_message.push('sfdg;lkjfd asd;lfkj asd alskdf 9e8rtg 98re oigf eroigu osd flj');
			_message.push('dfslkgj dsasdfklj we98uwe waefkj laksdjf');
			_message.push('asdflkasd asdfj lkasdf asdf ioasdjf ;lkasdf asldkfj lasdf');
			_message.push('asdfasdlkj asdf');
			_message.push('asdfert54');
			_message.push('dfgasdfas');
			_message.push(' asdfasdf asdf');
			_message.push('poiuvbmr kdsw456');
			
			trace('222+++');
		}
		
		/**
		 * получить рандомное сообщение
		 */
		private function getRandomMessage():String
		{
			var str:String = 'Привет,  солнце';
			
			var rnd:int = Math.floor(Math.random()*_message.length);
			str = _message[rnd];
			return str;
		}
		
		public function startTweenMove():void
		{
			moveSky1Right();
			moveSky2Right();
			moveSky3Right();
			moveSky4Right();
			moveSky5Right();
			moveSky6Right();
			
			sky_01.currentText = getRandomMessage();
			sky_02.currentText = getRandomMessage();
			sky_03.currentText = getRandomMessage();
			sky_04.currentText = getRandomMessage();
			sky_05.currentText = getRandomMessage();
			sky_06.currentText = getRandomMessage();
			sky_07.currentText = getRandomMessage();
			sky_08.currentText = getRandomMessage();
			sky_09.currentText = getRandomMessage();
			
			//
			sky_01.id = 1;
			sky_02.id = 2;
			sky_03.id = 3;
			sky_04.id = 4;
			sky_05.id = 5;
			sky_06.id = 6;
			sky_07.id = 7;
			sky_08.id = 8;
			sky_09.id = 9;
			
			
			moveSky01Right();
			moveSky02Right();
			moveSky03Right();
			moveSky04Right();
			moveSky05Right();
			moveSky06Right();
			moveSky07Right();
			moveSky08Right();
			moveSky09Right();
			
			
			addEventListener(KeyboardEventD.ON_CLICK_SKY_MESSAGE, onClickMessage, true);
		}
		
		public function stopTweenMove():void
		{
			removeEventListener(KeyboardEventD.ON_CLICK_SKY_MESSAGE, onClickMessage, true);
			
			_tweenSky1.clear();
			_tweenSky2.clear();
			_tweenSky3.clear();
			_tweenSky4.clear();
			_tweenSky5.clear();
			_tweenSky6.clear();
			//
			_tweenSky01.clear();
			_tweenSky02.clear();
			_tweenSky03.clear();
			_tweenSky04.clear();
			_tweenSky05.clear();
			_tweenSky06.clear();
			_tweenSky07.clear();
			_tweenSky08.clear();
			_tweenSky09.clear();
			
			
		}
		
		//	public
		public function destroy():void
		{
			stopTweenMove();
			
			_message = null;
			
		}
		
		public function updateDisplayList(sw:Number,sh:Number):void
		{
			
		}
		//	----------------------------------------------------------------------------------
		
		//	GETTERS/SETTERS
		
		//	----------------------------------------------------------------------------------
		
		//	HANDLERS
		// для 1 валового облака
		private function moveSky1Right():void {	_tweenSky1 = TweenLite.to(sky1, _time1, { x: SKY_GAP_RIGHT , ease:'none',	onComplete:onmoveSky1Right} ); }
		
		private function moveSky1Left():void {	_tweenSky1 = TweenLite.to(sky1, _time1, { x: SKY_GAP_LEFT , ease:'none',	onComplete:moveSky1Right } ); }
		// для 2 валового облака
		private function moveSky2Right():void{	_tweenSky2=TweenLite.to(sky2, _time1, { x: SKY_GAP_RIGHT ,ease:'none',	onComplete:onmoveSky2Right} );}
		private function moveSky2Left():void {	_tweenSky2 = TweenLite.to(sky2, _time1, { x: SKY_GAP_LEFT , ease:'none',	onComplete:moveSky2Right } ); }
		// для 3 валового облака
		private function moveSky3Right():void{	_tweenSky3=TweenLite.to(sky3, _time1, { x: SKY_GAP_RIGHT ,ease:'none',	onComplete:onmoveSky3Right} );}
		private function moveSky3Left():void{	_tweenSky3=TweenLite.to(sky3, _time1, { x: SKY_GAP_LEFT ,ease:'none',	onComplete:moveSky3Right} );}
		// для 4 валового облака
		private function moveSky4Right():void{	_tweenSky4=TweenLite.to(sky4, _time3, { x: SKY_GAP_RIGHT ,ease:'none',	onComplete:onmoveSky4Right} );}
		private function moveSky4Left():void{	_tweenSky4=TweenLite.to(sky4, _time3, { x: SKY_GAP_LEFT ,ease:'none',	onComplete:moveSky4Right} );}
		// для 5 валового облака
		private function moveSky5Right():void{	_tweenSky5=TweenLite.to(sky5, _time2, { x: SKY_GAP_RIGHT ,ease:'none',	onComplete:onmoveSky5Right} );}
		private function moveSky5Left():void {	_tweenSky5 = TweenLite.to(sky5, _time2, { x: SKY_GAP_LEFT , ease:'none',	onComplete:moveSky5Right } ); }
		// для 6 валового облака
		private function moveSky6Right():void{	_tweenSky6=TweenLite.to(	sky6, _time2, { x: SKY_GAP_RIGHT ,ease:'none',	onComplete:onmoveSky6Right} );}
		private function moveSky6Left():void {	_tweenSky6 = TweenLite.to(	sky6, _time2, { x: SKY_GAP_LEFT , ease:'none',	onComplete:moveSky6Right } ); }
		
		private function onmoveSky1Right():void
		{
			sky1.x = SKY_GAP_LEFT;
			moveSky1Right();
		}
		private function onmoveSky2Right():void
		{
			sky2.x = SKY_GAP_LEFT;
			moveSky2Right();
		}
		private function onmoveSky3Right():void
		{
			sky3.x = SKY_GAP_LEFT;
			moveSky3Right();
		}
		private function onmoveSky4Right():void
		{
			sky4.x = SKY_GAP_LEFT;
			moveSky4Right();
		}
		private function onmoveSky5Right():void
		{
			sky5.x = SKY_GAP_LEFT;
			moveSky5Right();
		}
		private function onmoveSky6Right():void
		{
			sky6.x = SKY_GAP_LEFT;
			moveSky6Right();
		}
		
		// облачка с сообщениями
		// sky 01
		private function moveSky01Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_01.x + SKY_GAP_RIGHT) / _speed1);
			//trace('time',time);
			_tweenSky01 = TweenLite.to(sky_01, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky01 } );
		}
		
		private function moveSky01Left():void
		{
			_tweenSky01 = TweenLite.to(sky_01, _time1, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky01 } );
		}
		
		private function onFinishRightSky01():void
		{
			sky_01.currentText = getRandomMessage();
			sky_01.x = SKY_M_GAP_LEFT;
			
			moveSky01Right();
		}
		
		private function onFinishLeftSky01():void
		{
			sky_01.currentText = getRandomMessage();
			moveSky01Right();
		}
		// sky 02
		private function moveSky02Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_02.x + SKY_GAP_RIGHT) / _speed2);
			//trace('time',time);
			_tweenSky02 = TweenLite.to(sky_02, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky02 } );
		}
		
		private function moveSky02Left():void
		{
			_tweenSky02 = TweenLite.to(sky_02, _time2, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky02 } );
		}
		
		private function onFinishRightSky02():void
		{
			sky_02.currentText = getRandomMessage();
			sky_02.x = SKY_M_GAP_LEFT;
			moveSky02Right();
		}
		
		private function onFinishLeftSky02():void
		{
			sky_02.currentText = getRandomMessage();
			moveSky02Right();
		}
		// sky 03
		private function moveSky03Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_03.x + SKY_GAP_RIGHT) / _speed3);
			//trace('time',time);
			_tweenSky03 = TweenLite.to(sky_03, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky03 } );
		}
		
		private function moveSky03Left():void
		{
			_tweenSky03 = TweenLite.to(sky_03, _time1, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky03 } );
		}
		
		private function onFinishRightSky03():void
		{
			sky_03.currentText = getRandomMessage();
			sky_03.x = SKY_M_GAP_LEFT;
			moveSky03Right();
		}
		
		private function onFinishLeftSky03():void
		{
			sky_03.currentText = getRandomMessage();
			moveSky03Right();
		}
		// sky 04
		private function moveSky04Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_04.x + SKY_GAP_RIGHT) / _speed1);
			//trace('time',time);
			_tweenSky04 = TweenLite.to(sky_04, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky04 } );
		}
		
		private function moveSky04Left():void
		{
			_tweenSky04 = TweenLite.to(sky_04, _time2, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky04 } );
		}
		
		private function onFinishRightSky04():void
		{
			sky_04.currentText = getRandomMessage();
			sky_04.x = SKY_M_GAP_LEFT;
			moveSky04Right();
		}
		
		private function onFinishLeftSky04():void
		{
			sky_04.currentText = getRandomMessage();
			moveSky04Right();
		}
		// sky 05
		private function moveSky05Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_05.x + SKY_GAP_RIGHT) / _speed2);
			//trace('time',time);
			_tweenSky05 = TweenLite.to(sky_05, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky05 } );
		}
		
		private function moveSky05Left():void
		{
			_tweenSky05 = TweenLite.to(sky_05, _time1, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky05 } );
		}
		
		private function onFinishRightSky05():void
		{
			sky_05.currentText = getRandomMessage();
			sky_05.x = SKY_M_GAP_LEFT;
			moveSky05Right();
		}
		
		private function onFinishLeftSky05():void
		{
			sky_05.currentText = getRandomMessage();
			moveSky05Right();
		}
		// sky 06
		private function moveSky06Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_06.x + SKY_GAP_RIGHT) / _speed3);
			//trace('time',time);
			_tweenSky06 = TweenLite.to(sky_06, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky06 } );
		}
		
		private function moveSky06Left():void
		{
			_tweenSky06 = TweenLite.to(sky_06, _time2, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky06 } );
		}
		
		private function onFinishRightSky06():void
		{
			sky_06.currentText = getRandomMessage();
			sky_06.x = SKY_M_GAP_LEFT;
			moveSky06Right();
		}
		
		private function onFinishLeftSky06():void
		{
			sky_06.currentText = getRandomMessage();
			moveSky06Right();
		}
		// sky 07
		private function moveSky07Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_07.x + SKY_GAP_RIGHT) / _speed1);
			//trace('time',time);
			_tweenSky07 = TweenLite.to(sky_07, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky07 } );
		}
		
		private function moveSky07Left():void
		{
			_tweenSky07 = TweenLite.to(sky_07, _time1, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky07 } );
		}
		
		private function onFinishRightSky07():void
		{
			sky_07.currentText = getRandomMessage();
			sky_07.x = SKY_M_GAP_LEFT;
			moveSky07Right();
		}
		
		private function onFinishLeftSky07():void
		{
			sky_07.currentText = getRandomMessage();
			moveSky07Right();
		}
		// sky 08
		private function moveSky08Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_08.x + SKY_GAP_RIGHT) / _speed2);
			//trace('time',time);
			_tweenSky08 = TweenLite.to(sky_08, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky08 } );
		}
		
		private function moveSky08Left():void
		{
			_tweenSky08 = TweenLite.to(sky_08, _time2, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky08 } );
		}
		
		private function onFinishRightSky08():void
		{
			sky_08.currentText = getRandomMessage();
			sky_08.x = SKY_M_GAP_LEFT;
			moveSky08Right();
		}
		
		private function onFinishLeftSky08():void
		{
			sky_08.currentText = getRandomMessage();
			moveSky08Right();
		}
		// sky 09
		private function moveSky09Right():void
		{
			var time:Number = Math.round((ApplicationMediator.SCREEN_WIDTH - sky_09.x + SKY_GAP_RIGHT) / _speed3);
			//trace('time',time);
			_tweenSky09 = TweenLite.to(sky_09, time, { x: SKY_M_GAP_RIGHT , ease:'none',	onComplete:onFinishRightSky09 } );
		}
		
		private function moveSky09Left():void
		{
			_tweenSky09 = TweenLite.to(sky_09, _time1, { x: SKY_M_GAP_LEFT , ease:'none',	onComplete:onFinishLeftSky09 } );
		}
		
		private function onFinishRightSky09():void
		{
			sky_09.currentText = getRandomMessage();
			sky_09.x = SKY_M_GAP_LEFT;
			moveSky09Right();
		}
		
		private function onFinishLeftSky09():void
		{
			sky_09.currentText = getRandomMessage();
			moveSky09Right();
		}
		
		private function onClickMessage(e:KeyboardEventD):void 
		{
			////trace('id', e.id);
			var time:Number = 3;
			switch (e.id) 
			{
				case 1:
					_tweenSky01 = TweenLite.to(sky_01, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky01 } );
				break;
				
				case 2:
					_tweenSky02 = TweenLite.to(sky_02, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky02 } );
				break;
				
				case 3:
					_tweenSky03 = TweenLite.to(sky_03, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky03 } );
				break;
				
				case 4:
					_tweenSky04 = TweenLite.to(sky_04, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky04 } );
				break;
				
				case 5:
					_tweenSky05 = TweenLite.to(sky_05, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky05 } );
				break;
				
				case 6:
					_tweenSky06 = TweenLite.to(sky_06, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky06 } );
				break;
				
				case 7:
					_tweenSky07 = TweenLite.to(sky_07, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky07 } );
				break;
				
				case 8:
					_tweenSky08 = TweenLite.to(sky_08, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky08 } );
				break;
				
				case 9:
					_tweenSky09 = TweenLite.to(sky_09, time, { y:SKY_M_GAP_OUT_Y , ease:'none', onComplete:onFinishOutSky09 } );
				break;
				
			}
		}
		// 01
		private function onFinishOutSky01():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('01',rnd_Y);
			
			sky_01.currentText = getRandomMessage();
			
			sky_01.x = SKY_GAP_LEFT;
			sky_01.y = rnd_Y;
			moveSky01Right();
			
		}
		// 02
		private function onFinishOutSky02():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('02',rnd_Y);
			
			sky_02.currentText = getRandomMessage();
			
			sky_02.x = SKY_GAP_LEFT;
			sky_02.y = rnd_Y;
			moveSky02Right();
		}
		// 03
		private function onFinishOutSky03():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('03',rnd_Y);
			
			sky_03.currentText = getRandomMessage();
			
			sky_03.x = SKY_GAP_LEFT;
			sky_03.y = rnd_Y;
			moveSky03Right();
		}
		// 04
		private function onFinishOutSky04():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('04',rnd_Y);
			
			sky_04.currentText = getRandomMessage();
			
			sky_04.x = SKY_GAP_LEFT;
			sky_04.y = rnd_Y;
			moveSky04Right();
		}
		// 05
		private function onFinishOutSky05():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('05',rnd_Y);
			
			sky_05.currentText = getRandomMessage();
			
			sky_05.x = SKY_GAP_LEFT;
			sky_05.y = rnd_Y;
			moveSky05Right();
		}
		// 06
		private function onFinishOutSky06():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('06',rnd_Y);
			
			sky_06.currentText = getRandomMessage();
			
			sky_06.x = SKY_GAP_LEFT;
			sky_06.y = rnd_Y;
			moveSky06Right();
		}
		// 07
		private function onFinishOutSky07():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('07',rnd_Y);
			
			sky_07.currentText = getRandomMessage();
			
			sky_07.x = SKY_GAP_LEFT;
			sky_07.y = rnd_Y;
			moveSky07Right();
		}
		// 08
		private function onFinishOutSky08():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('08',rnd_Y);
			
			sky_08.currentText = getRandomMessage();
			
			sky_08.x = SKY_GAP_LEFT;
			sky_08.y = rnd_Y;
			moveSky08Right();
		}
		// 09
		private function onFinishOutSky09():void
		{
			var rnd:int = Math.floor(Math.random()-0.5);
			var rnd_Y:int = Math.round(Math.round(Math.random()*100)/100*bgdSky.height);
			
			////trace('09',rnd_Y);
			
			sky_09.currentText = getRandomMessage();
			
			sky_09.x = SKY_GAP_LEFT;
			sky_09.y = rnd_Y;
			moveSky09Right();
			
		}
		
	}

}